<?php
/*
	This SQL query will create the table to store your object.

	CREATE TABLE `diff_record` (
	`diff_recordid` int(11) NOT NULL auto_increment,
	`wiki_pageid` int(11) NOT NULL,
	`diff_identifierid` int(11) NOT NULL,
	`views` INT NOT NULL, INDEX(`wiki_pageid`,`diff_identifierid`), PRIMARY KEY  (`diff_recordid`)) ENGINE=MyISAM;
*/

/**
* <b>diff_record</b> class with integrated CRUD methods.
* @author Php Object Generator
* @version POG 3.0f / PHP5
* @copyright Free for personal & commercial use. (Offered under the BSD license)
* @link http://www.phpobjectgenerator.com/?language=php5&wrapper=pog&objectName=diff_record&attributeList=array+%28%0A++0+%3D%3E+%27wiki_page%27%2C%0A++1+%3D%3E+%27diff_identifier%27%2C%0A++2+%3D%3E+%27views%27%2C%0A%29&typeList=array+%28%0A++0+%3D%3E+%27BELONGSTO%27%2C%0A++1+%3D%3E+%27BELONGSTO%27%2C%0A++2+%3D%3E+%27INT%27%2C%0A%29
*/
include_once('class.pog_base.php');
class diff_record extends POG_Base
{
	public $diff_recordId = '';

	/**
	 * @var INT(11)
	 */
	public $wiki_pageId;
	
	/**
	 * @var INT(11)
	 */
	public $diff_identifierId;
	
	/**
	 * @var INT
	 */
	public $views;
	
	public $pog_attribute_type = array(
		"diff_recordId" => array('db_attributes' => array("NUMERIC", "INT")),
		"wiki_page" => array('db_attributes' => array("OBJECT", "BELONGSTO")),
		"diff_identifier" => array('db_attributes' => array("OBJECT", "BELONGSTO")),
		"views" => array('db_attributes' => array("NUMERIC", "INT")),
		);
	public $pog_query;
	
	
	/**
	* Getter for some private attributes
	* @return mixed $attribute
	*/
	public function __get($attribute)
	{
		if (isset($this->{"_".$attribute}))
		{
			return $this->{"_".$attribute};
		}
		else
		{
			return false;
		}
	}
	
	function diff_record($views='')
	{
		$this->views = $views;
	}
	
	
	/**
	* Gets object from database
	* @param integer $diff_recordId 
	* @return object $diff_record
	*/
	function Get($diff_recordId)
	{
		$connection = Database::Connect();
		$this->pog_query = "select * from `diff_record` where `diff_recordid`='".intval($diff_recordId)."' LIMIT 1";
		$cursor = Database::Reader($this->pog_query, $connection);
		while ($row = Database::Read($cursor))
		{
			$this->diff_recordId = $row['diff_recordid'];
			$this->wiki_pageId = $row['wiki_pageid'];
			$this->diff_identifierId = $row['diff_identifierid'];
			$this->views = $this->Unescape($row['views']);
		}
		return $this;
	}
	
	
	/**
	* Returns a sorted array of objects that match given conditions
	* @param multidimensional array {("field", "comparator", "value"), ("field", "comparator", "value"), ...} 
	* @param string $sortBy 
	* @param boolean $ascending 
	* @param int limit 
	* @return array $diff_recordList
	*/
	function GetList($fcv_array = array(), $sortBy='', $ascending=true, $limit='')
	{
		$connection = Database::Connect();
		$sqlLimit = ($limit != '' ? "LIMIT $limit" : '');
		$this->pog_query = "select * from `diff_record` ";
		$diff_recordList = Array();
		if (sizeof($fcv_array) > 0)
		{
			$this->pog_query .= " where ";
			for ($i=0, $c=sizeof($fcv_array); $i<$c; $i++)
			{
				if (sizeof($fcv_array[$i]) == 1)
				{
					$this->pog_query .= " ".$fcv_array[$i][0]." ";
					continue;
				}
				else
				{
					if ($i > 0 && sizeof($fcv_array[$i-1]) != 1)
					{
						$this->pog_query .= " AND ";
					}
					if (isset($this->pog_attribute_type[$fcv_array[$i][0]]['db_attributes']) && $this->pog_attribute_type[$fcv_array[$i][0]]['db_attributes'][0] != 'NUMERIC' && $this->pog_attribute_type[$fcv_array[$i][0]]['db_attributes'][0] != 'SET')
					{
						if ($GLOBALS['configuration']['db_encoding'] == 1)
						{
							$value = POG_Base::IsColumn($fcv_array[$i][2]) ? "BASE64_DECODE(".$fcv_array[$i][2].")" : "'".$fcv_array[$i][2]."'";
							$this->pog_query .= "BASE64_DECODE(`".$fcv_array[$i][0]."`) ".$fcv_array[$i][1]." ".$value;
						}
						else
						{
							$value =  POG_Base::IsColumn($fcv_array[$i][2]) ? $fcv_array[$i][2] : "'".$this->Escape($fcv_array[$i][2])."'";
							$this->pog_query .= "`".$fcv_array[$i][0]."` ".$fcv_array[$i][1]." ".$value;
						}
					}
					else
					{
						$value = POG_Base::IsColumn($fcv_array[$i][2]) ? $fcv_array[$i][2] : "'".$fcv_array[$i][2]."'";
						$this->pog_query .= "`".$fcv_array[$i][0]."` ".$fcv_array[$i][1]." ".$value;
					}
				}
			}
		}
		if ($sortBy != '')
		{
			if (isset($this->pog_attribute_type[$sortBy]['db_attributes']) && $this->pog_attribute_type[$sortBy]['db_attributes'][0] != 'NUMERIC' && $this->pog_attribute_type[$sortBy]['db_attributes'][0] != 'SET')
			{
				if ($GLOBALS['configuration']['db_encoding'] == 1)
				{
					$sortBy = "BASE64_DECODE($sortBy) ";
				}
				else
				{
					$sortBy = "$sortBy ";
				}
			}
			else
			{
				$sortBy = "$sortBy ";
			}
		}
		else
		{
			$sortBy = "diff_recordid";
		}
		$this->pog_query .= " order by ".$sortBy." ".($ascending ? "asc" : "desc")." $sqlLimit";
		$thisObjectName = get_class($this);
		$cursor = Database::Reader($this->pog_query, $connection);
		while ($row = Database::Read($cursor))
		{
			$diff_record = new $thisObjectName();
			$diff_record->diff_recordId = $row['diff_recordid'];
			$diff_record->wiki_pageId = $row['wiki_pageid'];
			$diff_record->diff_identifierId = $row['diff_identifierid'];
			$diff_record->views = $this->Unescape($row['views']);
			$diff_recordList[] = $diff_record;
		}
		return $diff_recordList;
	}
	
	
	/**
	* Saves the object to the database
	* @return integer $diff_recordId
	*/
	function Save()
	{
		$connection = Database::Connect();
		$this->pog_query = "select `diff_recordid` from `diff_record` where `diff_recordid`='".$this->diff_recordId."' LIMIT 1";
		$rows = Database::Query($this->pog_query, $connection);
		if ($rows > 0)
		{
			$this->pog_query = "update `diff_record` set 
			`wiki_pageid`='".$this->wiki_pageId."', 
			`diff_identifierid`='".$this->diff_identifierId."', 
			`views`='".$this->Escape($this->views)."' where `diff_recordid`='".$this->diff_recordId."'";
		}
		else
		{
			$this->pog_query = "insert into `diff_record` (`wiki_pageid`, `diff_identifierid`, `views` ) values (
			'".$this->wiki_pageId."', 
			'".$this->diff_identifierId."', 
			'".$this->Escape($this->views)."' )";
		}
		$insertId = Database::InsertOrUpdate($this->pog_query, $connection);
		if ($this->diff_recordId == "")
		{
			$this->diff_recordId = $insertId;
		}
		return $this->diff_recordId;
	}
	
	
	/**
	* Clones the object and saves it to the database
	* @return integer $diff_recordId
	*/
	function SaveNew()
	{
		$this->diff_recordId = '';
		return $this->Save();
	}
	
	
	/**
	* Deletes the object from the database
	* @return boolean
	*/
	function Delete()
	{
		$connection = Database::Connect();
		$this->pog_query = "delete from `diff_record` where `diff_recordid`='".$this->diff_recordId."'";
		return Database::NonQuery($this->pog_query, $connection);
	}
	
	
	/**
	* Deletes a list of objects that match given conditions
	* @param multidimensional array {("field", "comparator", "value"), ("field", "comparator", "value"), ...} 
	* @param bool $deep 
	* @return 
	*/
	function DeleteList($fcv_array)
	{
		if (sizeof($fcv_array) > 0)
		{
			$connection = Database::Connect();
			$pog_query = "delete from `diff_record` where ";
			for ($i=0, $c=sizeof($fcv_array); $i<$c; $i++)
			{
				if (sizeof($fcv_array[$i]) == 1)
				{
					$pog_query .= " ".$fcv_array[$i][0]." ";
					continue;
				}
				else
				{
					if ($i > 0 && sizeof($fcv_array[$i-1]) !== 1)
					{
						$pog_query .= " AND ";
					}
					if (isset($this->pog_attribute_type[$fcv_array[$i][0]]['db_attributes']) && $this->pog_attribute_type[$fcv_array[$i][0]]['db_attributes'][0] != 'NUMERIC' && $this->pog_attribute_type[$fcv_array[$i][0]]['db_attributes'][0] != 'SET')
					{
						$pog_query .= "`".$fcv_array[$i][0]."` ".$fcv_array[$i][1]." '".$this->Escape($fcv_array[$i][2])."'";
					}
					else
					{
						$pog_query .= "`".$fcv_array[$i][0]."` ".$fcv_array[$i][1]." '".$fcv_array[$i][2]."'";
					}
				}
			}
			return Database::NonQuery($pog_query, $connection);
		}
	}
	
	
	/**
	* Associates the wiki_page object to this one
	* @return boolean
	*/
	function GetWiki_page()
	{
		$wiki_page = new wiki_page();
		return $wiki_page->Get($this->wiki_pageId);
	}
	
	
	/**
	* Associates the wiki_page object to this one
	* @return 
	*/
	function SetWiki_page(&$wiki_page)
	{
		$this->wiki_pageId = $wiki_page->wiki_pageId;
	}
	
	
	/**
	* Associates the diff_identifier object to this one
	* @return boolean
	*/
	function GetDiff_identifier()
	{
		$diff_identifier = new diff_identifier();
		return $diff_identifier->Get($this->diff_identifierId);
	}
	
	
	/**
	* Associates the diff_identifier object to this one
	* @return 
	*/
	function SetDiff_identifier(&$diff_identifier)
	{
		$this->diff_identifierId = $diff_identifier->diff_identifierId;
	}
}
?>